package com.platform.common.utils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @className: Base64Util
 * @description: Base64 编码工具
 * @date: 2022/8/8
 * @author: chengqiuming
 */
public class Base64Util {
    /**
     * 编码
     *
     * @param str 编码前字符串
     * @return 编码后字符串
     */
    public static String encode(String str) {
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] b = str.getBytes(StandardCharsets.UTF_8);
        return encoder.encodeToString(b);
    }

    /**
     * 解码
     *
     * @param s 解密前字符串
     * @return 解密后字符串
     */
    public static String decode(String s) {
        Base64.Decoder decoder = Base64.getDecoder();
        return new String(decoder.decode(s), StandardCharsets.UTF_8);
    }
}
